'use strict';
module.exports = {
	/**
	 * 查询详情
	 * @url client/article/pub/getDetails 前端调用的url参数地址
	 * data 请求参数 说明
	 * @param {String} _id  ID
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let { _id } = data;
		
		if(vk.pubfn.isNull(_id)) {
			return { code: -1, msg: "_id不能为空" }
		}

		let dbName = "db-article";
		
		res["data"] = await vk.baseDao.selects({
			dbName,
			getOne: true,
			getMain: true,
			whereJson: {
				_id: _id
			},
			fieldJson: {
				wx_openid: false,
				uid: false
			},
			foreignDB: [
				{
					dbName: "uni-id-users",
					localKey:"uid",
					foreignKey:"_id",
					as:"userInfo",
					limit:1,
					fieldJson: {
						nickname: true,
						avatar: true,
						intro: true
					}
				},
				{
					dbName: "db-praise",
					localKey: "_id",
					foreignKey: "article_id",
					as: "is_praise",
					limit: 1,
					whereJson: {
						wx_openid: !vk.pubfn.isNull(userInfo)? userInfo.wx_openid: ''
					}
				},
				{
					dbName: "db-attention",
					localKey: "uid",
					foreignKey: "author_id",
					as: "is_attention",
					limit: 1,
					whereJson: {
						uid: !vk.pubfn.isNull(userInfo)? userInfo._id: ''
					}
				},
				{
					dbName: "db-collect",
					localKey: "_id",
					foreignKey: "collect_id",
					as: "is_collect",
					limit: 1,
					whereJson: {
						uid: !vk.pubfn.isNull(userInfo)? userInfo._id: ''
					}
				}
			]
		})
		
		if(vk.pubfn.isNull(res.data)) {
			return { code: 404, msg: "访问不存在或已下架"}
		}
		
		let num = await vk.baseDao.update({
			dbName,
			whereJson: {
				_id: _id
			},
			dataJson: {
				visit_num: _.inc(1)
			}
		})
		
		if("is_praise" in res.data) {
			res.data["is_praise"] = res.data.is_praise.status;
		} else {
			res.data["is_praise"] = 0;
		}
		
		if("is_attention" in res.data) {
			res.data["is_attention"] = Object.keys(res.data.is_attention).length !== 0? 1: 0;
		} else {
			res.data["is_attention"] = 0;
		}
		
		if("is_collect" in res.data) {
			res.data["is_collect"] = Object.keys(res.data.is_collect).length !== 0? 1: 0;
		} else {
			res.data["is_collect"] = 0;
		}
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
